package com.blackline.UI.MATCH;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;

import com.blackline.auto.Match.Match_smoketest;

/**
 * @author XBO
 * @version Jan, 2013
 * <p>This page is after clicking "Craete Data Source" button on the Create Data Source page.
 * Please refer to UI.CreateDataSourceSubpage.png in doc-files.
 */
public class CreateDataSourceSubpage {
	private static Logger logger=Logger.getLogger(CreateDataSourceSubpage.class);
	

	
	/**
	 * Name iframe. If you want to input texts into Name text box, you need to switch into this iframe; later you may need to switch back to defaultContent().
	 */
	@FindBy (name = "TableDefinitionView")
	public WebElement Name_iframe;
	
	
	/**
	 * Name text field
	 */
	@FindBy(id = "ctl00_ctl00_contentBody_cphMain_tbName")
	public WebElement Name_textField;
	
	
	/**
	 * Description text field
	 */
	@FindBy(id = "ctl00_ctl00_contentBody_cphMain_lblDesc")
	public WebElement Description_textField;
	
	/**
	 * Team text field
	 */
	@FindBy(id = "ctl00_ctl00_contentBody_cphMain_lblTeam")
	public WebElement Team_textField;
	
	/**
	 * Save button
	 */
	@FindBy(id = "ctl00_ctl00_contentBody_cphMain_btnSave")
	public WebElement Save_button;
	
	/**
	 * Cancel button
	 */
	@FindBy(id = "ctl00_ctl00_contentBody_cphMain_btnCancel")
	public WebElement Cancel_button;
	
	/**
	 * Edit Selected button
	 */
	@FindBy(partialLinkText = "Edit Selected")
	public WebElement Edit_Selected_button;
	
	/**
	 * Update button
	 */
	@FindBy(partialLinkText = "Update")
	public WebElement Update_button;
	
	/**
	 * Cancel Editing button
	 */
	@FindBy(partialLinkText = "Cancel Editing")
	public WebElement Cancel_Editing_button;
	
	/**
	 * Delete Selected button
	 */
	@FindBy(partialLinkText = "Delete Selected")
	public WebElement Delete_Selected_button;
	
	/**
	 * The table
	 */
	@FindBy (id = "ctl00_ctl00_contentBody_cphMain_RadGridColumnDefines_ctl00")
	public WebElement _the_table;
	
	/**
	 * "Amount" cell in Edit mode
	 */
	@FindBy (xpath = "//input[@value='Amount']")
	public WebElement Amount_cellInEditMode;
	
	/**
	 * "Amount" cell's data type drop-down list in Edit mode
	 */
//	@FindBy (xpath = "//input[@value='Amount']/../following::td[1]/input")
	@FindBy (id = "ctl00_ctl00_contentBody_cphMain_RadGridColumnDefines_ctl00_ctl18_typeCombo_Input")
	public WebElement Amount_DataType_inEditMode;
	
	/**
	 * "ReverseAmount" cell in Edit mode
	 */
	@FindBy (xpath = "//input[@value='ReverseAmount'")
	public WebElement ReverseAmount_cellInEditMode;
	
	/**
	 * "ReverseAmount" cell's data type drop-down list in Edit mode
	 */
//	@FindBy (xpath = "//input[@value='ReverseAmount']/../following::td[1]/div/table/tbody/tr/td/input")
	@FindBy (id = "ctl00_ctl00_contentBody_cphMain_RadGridColumnDefines_ctl00_ctl22_typeCombo_Input")
	public WebElement ReverseAmount_DataType_inEditMode;
	
	/**
	 * "CheckDollarAmount" cell's data type drop-down list in Edit mode
	 */
	@FindBy (id = "ctl00_ctl00_contentBody_cphMain_RadGridColumnDefines_ctl00_ctl08_typeCombo_Input")
	public WebElement CheckDollarAmount_DataType_inEditMode;
	
	/**
	 * Any cell's data type drop down list in Edit mode
	 */
	
	
	
	/**
	 * Select item(s) from "Create Data Source" table
	 * @param driver WebDriver
	 * @param itemsToSelect Items you want to select.<br>
	 * Sample:<br>
	 * UI.CreateDataSourceSubpage.selectItemsFromTable(driver, "Account"); //Select "Account" item.<br>
	 * UI.CreateDataSourceSubpage.selectItemsFromTable(driver, "Amount", "Account"); //Select "Account" and "Amount".
	 */
	public void selectItemsFromTable(WebDriver driver, String... itemsToSelect)
	{
		Actions action = new Actions(driver);
		action.keyDown(Keys.CONTROL);
        if (itemsToSelect.length == 0) {
            logger.info("No item is set to be selected.");
        }
        for (int i = 0; i < itemsToSelect.length; i++) {
        	logger.info("Going to select: " + itemsToSelect[i]);
            action.moveToElement(driver.findElement(By.xpath("(//span[contains(text(),'"+ itemsToSelect[i]+"')])")));
            action.click().perform();
        }
        action.keyUp(Keys.CONTROL);
	}
	
}
